package GuiClasses;
/**
 * TCSS 360 - Project
 */
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.util.Observable;
import java.util.Observer;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import objectClasses.User;
import objectClasses.Attendees;
import objectClasses.Attendee;
/**
 *  Creates a registration panel that contains all of the objects needed for registering for an event.
 *  
 *  @author Abraham Santini
 */
public class RegistrationPanel extends Observable {
	/**
	 * The panel that hold the registration information
	 */
	private JPanel my_panel;
	
	/**
	 * Public constructor for a Registration Panel.
	 * @param the_o_er The Observer object that will be updated.  This should
	 * be a BeWeaveFrame.
	 */
	
	public RegistrationPanel(final Observer the_o_er) {
		addObserver(the_o_er);
		my_panel = new JPanel();
		
		my_panel.setLayout(new BoxLayout(my_panel, BoxLayout.Y_AXIS));
		my_panel.add(Box.createVerticalGlue());
		
		// creates the first name panel
		JPanel firstNamePanel = new JPanel();
		firstNamePanel.setLayout(new BoxLayout(firstNamePanel, BoxLayout.X_AXIS));
		firstNamePanel.add(Box.createVerticalGlue());
		firstNamePanel.add(new JLabel("First Name"));
		final JTextField firstField = new JTextField();
		firstNamePanel.add(firstField);
		firstNamePanel.add(Box.createVerticalGlue());
		my_panel.add(firstNamePanel);
		
		// creates the last name panel
		JPanel lastNamePanel = new JPanel();
		lastNamePanel.setLayout(new BoxLayout(lastNamePanel, BoxLayout.X_AXIS));
		lastNamePanel.add(Box.createVerticalGlue());
		lastNamePanel.add(new JLabel("Last Name"));
		final JTextField lastField = new JTextField();
		lastNamePanel.add(lastField);
		lastNamePanel.add(Box.createVerticalGlue());
		my_panel.add(lastNamePanel);
		// creates the email address panel
		JPanel emailPanel = new JPanel();
		emailPanel.setLayout(new BoxLayout(emailPanel, BoxLayout.X_AXIS));
		emailPanel.add(Box.createVerticalGlue());
		emailPanel.add(new JLabel("Email Address"));
		final JTextField addyField = new JTextField();
		emailPanel.add(addyField);
		emailPanel.add(Box.createVerticalGlue());
		my_panel.add(emailPanel);
		// creates the user name panel
		JPanel userNamePanel = new JPanel();
		userNamePanel.setLayout(new BoxLayout(userNamePanel, BoxLayout.X_AXIS));
		userNamePanel.add(Box.createVerticalGlue());
		userNamePanel.add(new JLabel("User Name"));
		final JTextField userField = new JTextField();
		userNamePanel.add(userField);
		userNamePanel.add(Box.createVerticalGlue());
		my_panel.add(userNamePanel);
		// creates the password panel
		JPanel passwordPanel = new JPanel();
		passwordPanel.setLayout(new BoxLayout(passwordPanel, BoxLayout.X_AXIS));
		passwordPanel.add(Box.createVerticalGlue());
		passwordPanel.add(new JLabel("Password"));
		final JTextField passField = new JTextField();
		passwordPanel.add(passField);
		passwordPanel.add(Box.createVerticalGlue());
		my_panel.add(passwordPanel);
		JButton registerButton = new JButton("Register");
		
		// action listener for registration button.
		registerButton.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				// TODO Auto-generated method stub
				try {
					GuiMain.setUser(new Attendee("A", firstField.getText(), lastField.getText(),
							addyField.getText(), userField.getText()));
					User.Register(userField.getText(), passField.getText(), addyField.getText(), firstField.getText(), 
							lastField.getText(), "0000000", 'A');
					System.out.println(((Attendees)GuiMain.getUser()).getUsername());
					setChanged();
					notifyObservers(new AttendeePanel(the_o_er).getPanel());
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
					System.out.println("Could not register user.  Try again");
				}
			}
			
		});
		
		my_panel.add(registerButton);
		my_panel.add(Box.createVerticalGlue());
		
	}
	/**
	 * Return the panel.
	 * @return my_panel The panel that contains the registration information.
	 */
	public JPanel getPanel() {
		return my_panel;
	}
	
}
